<?php

namespace App\Http\Controllers;

use App\Models\Chapter;
use App\Models\Repository;
use Illuminate\Http\Request;

class RepositoryPullController extends Controller
{
    public function index(Repository $repository)
    {
        $repository = $repository->load(['pulls', 'pulls.user', 'pulls.comments', 'pulls.comments.user']);

        return view('resources.repository.pull.index')->with('repository', $repository);
    }

    public function merge(Request $request, Chapter $chapter, Repository $repository)
    {
//        $repository = $repository->findOrFail($repository_id);
        $chapter = $chapter->findOrFail($request->chapter_id);
//        $this->authorize('repositories.pull', $repository);
        $repository->attachChapter($chapter, $request->sequence);
        $repository->pulls()->detach($request->chapter_id);
        return back();
//        return dd($repository->pulls()->wherePivot('chapter_id', $request->chapter_id)->first());
    }
}
